package com.yuxi.service.impl;

import com.yuxi.common.StringUtil;
import com.yuxi.common.SkList;
import com.yuxi.model.GroupRights;
import com.yuxi.modelExt.GroupRightsExt;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.service.IGroupRightsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.yuxi.modelExt.PermissionGroupExt;
import com.yuxi.dao.IPermissionGroupDao;
import com.yuxi.searchExt.PermissionGroupSearchExt;
import com.yuxi.pubInter.IBaseDao;
import com.yuxi.pubInter.AbstractBaseService;
import com.yuxi.service.IPermissionGroupService;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
*    @author sunke
*    @Date 2023-10-13 10:05:04
*    @description PermissionGroupService      权限组
*/
@Service(value = "permissionGroupService")
@Slf4j
public class PermissionGroupServiceImpl extends AbstractBaseService<PermissionGroupExt, PermissionGroupSearchExt,String> implements IPermissionGroupService {

    @Resource(name = "permissionGroupDao")
    private IPermissionGroupDao permissionGroupDao;

    @Resource
    private IGroupRightsService groupRightsService;

    @Override
    public IBaseDao<PermissionGroupExt, PermissionGroupSearchExt,String> getDao() {
        return permissionGroupDao;
    }
    @Override
    public void fullOtherData(List<PermissionGroupExt> list, Set<String> fullConfigSet) {
        super.fullOtherData(list, fullConfigSet);
        fullGroupRightsList(list, fullConfigSet);
    }


    private void fullGroupRightsList(List<PermissionGroupExt> list, Set<String> fullConfigSet) {
        try {
            if (StringUtil.isNullOrEmpty(list)) {
                return;
            }

            List<String> groupIds = list.stream().map(PermissionGroupExt::getId).collect(Collectors.toList());

            List<GroupRightsExt> groupRightsList = groupRightsService.getAllByInField(groupIds, GroupRights.GROUP_ID, fullConfigSet);
            if (StringUtil.isNullOrEmpty(groupRightsList)) {
                return;
            }

            Map<String, List<GroupRightsExt>> dataMap = groupRightsList.stream().collect(Collectors.groupingBy(GroupRightsExt::getGroupId));

            list.stream().forEach(item -> {
                try {
                    item.setGroupRightsList(dataMap.get(item.getId()));
                } catch (Exception e) {

                }


            });


        } catch (Exception e) {

        }
    }

    @Override
    public void afterInsert(PermissionGroupExt obj) throws SkException {
        super.afterInsert(obj);
        addRights(obj);

    }


    private void addRights(PermissionGroupExt obj) throws SkException {


        List<String> rightsIds = obj.getRightsIds();
        List<GroupRightsExt> groupRightsList = SkList.getInstance();
        if (StringUtil.isNotObjEmpty(rightsIds)) {
            rightsIds.stream().forEach(item -> {
                GroupRightsExt groupRights = new GroupRightsExt();
                groupRights.setGroupId(obj.getId());
                groupRights.setRightsId(item);
                groupRightsList.add(groupRights);

            });
            if (StringUtil.isNotObjEmpty(groupRightsList)) {
                groupRightsService.batchInsert(groupRightsList);
            }
        }
    }

    @Override
    public void afterUpdate(PermissionGroupExt obj) throws SkException {
        super.afterUpdate(obj);

        List<String> rightsIds = obj.getRightsIds();

        if (rightsIds != null) {
            groupRightsService.clearByGroupId(obj.getId());
            addRights(obj);
        }


    }

    @Override
    public void afterDelete(String id) throws SkException {
        super.afterDelete(id);
        groupRightsService.clearByGroupId(id);
    }



}